package com.itheima.dao;

import com.github.pagehelper.Page;
import com.itheima.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

public interface OrderDao_scg {

    @Insert("insert into t_order(member_id,orderDate,orderType,orderStatus,setmeal_id) values (#{memberId},#{orderDate},#{orderType},#{orderStatus},#{setmealId})")
    @Options(useGeneratedKeys = true)
    public void add(Order order);

    @Select("select * from t_order where id = #{id} and member_id = #{memberId} and orderDate = #{orderDate} and orderType = #{orderType} and orderStatus = #{orderStatus} and setmeal_id = #{setmealId}")
    public List<Order> findByCondition(Order order);

    @Select("select m.name member ,s.name setmeal,o.orderDate orderDate,o.orderType orderType from t_order o,t_member m,t_setmeal s where o.member_id=m.id and o.setmeal_id=s.id and o.id=#{id}")
    public Map findById4Detail(Integer id);

    @Select("select count(id) from t_order where orderDate = #{date}")
    public Integer findOrderCountByDate(String date);

    @Select("select count(id) from t_order where orderDate >= #{date}")
    public Integer findOrderCountAfterDate(String date);

    @Select("select count(id) from t_order where orderDate = #{date} and orderStatus = '已到诊'")
    public Integer findVisitsCountByDate(String date);

    @Select("select count(id) from t_order where orderDate = #{date} and orderStatus = '已到诊'")
    public Integer findVisitsCountAfterDate(String date);

    @Select("select s.name, count(o.id) setmeal_count , count(o.id)/(select count(id) from t_order) proportion from t_order o inner join t_setmeal s on s.id = o.setmeal_id group by o.setmeal_id order by setmeal_count desc limit 0,4")
    public List<Map> findHotSetmeal();

    @Select("SELECT * FROM t_order o,t_member m WHERE o.member_id=m.id AND (NAME LIKE '%${value}%' OR phoneNumber LIKE '%${value}%')")
    Page<Map> findOrderAndMember(String queryString);

    @Select("SELECT * FROM t_order o LEFT JOIN t_member m ON o.member_id=m.id")
    Page<Map> findAll();

    @Update("update t_order set orderDate=#{orderDate},orderType=#{orderType},orderStatus=#{orderStatus} where id=#{id}")
    void update(@Param("orderDate") Date orderDate, @Param("orderType") String orderType, @Param("orderStatus") String orderStatus, @Param("id") Integer id);

    @Select("SELECT * FROM t_order o,t_member m WHERE o.member_id=m.id AND (o.id=#{#id})")
    Map find(int id);

    @Select("select orderDate from t_order where id = #{id}")
    Date findDete(int id);

    @Delete("delete from t_order where id = #{id}")
    void deleteById(int id);

    @Update("update t_order set orderStatus = #{orderStatus} where id =#{id}")
    void updateOrderStatus(@Param("id") int id, @Param("orderStatus") String orderStatus);

    @Select("SELECT COUNT(*) FROM t_order o,t_member m WHERE o.member_id=m.id AND (NAME LIKE '%${value}%' OR phoneNumber LIKE '%${value}%')")
    Long findOrderAndMember2(String queryString);

    @Select("SELECT count(*) FROM t_order o LEFT JOIN t_member m ON o.member_id=m.id")
    Long findAll2();

    @Select("select member_id from t_order where id =#{oid}")
    int findById(int oid);
}
